<template>
  <div class="execution">
    <basic-container>
      <el-tabs v-model="activeName" @tab-click="handleClickTab">
        <el-tab-pane label="店铺标签" name="first"></el-tab-pane>
        <el-tab-pane label="平台标签" name="second"></el-tab-pane>

        <el-card class="filter-container" shadow="never">
          <div style="margin-top: 10px">
            <el-form :inline="true" size="small" label-width="140px">
              <el-form-item label="商家名称：" v-if="activeName === 'first'">
                <el-select
                  clearable
                  v-model="listQuery.storeId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.storeId"
                    :label="item.storeName"
                    :value="item.storeId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="平台名称：" v-if="activeName === 'second'">
                <el-select
                  clearable
                  v-model="appQuery.appId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click="handleSecrch" type="primary" size="small"
                  >查询结果</el-button
                >
                <el-button @click="handleResetSearch" size="small">
                  重置
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
        <el-card
          style="margin-top: 10px"
          shadow="never"
          v-if="activeName === 'second'"
        >
          <el-button
            class="btn-add"
            icon="el-icon-plus"
            type="primary"
            @click="handleAdd()"
            size="mini"
          >
            添加
          </el-button>
        </el-card>
        <div style="margin-top: 10px">
          <el-table
            :data="list"
            border
            :header-cell-style="{ color: '#4D4D4D', background: '#fafafa' }"
          >
            <el-table-column
              label="商家名称"
              align="center"
              prop="storeName"
            ></el-table-column>
            <el-table-column
              label="标签名称"
              align="center"
              prop="labelName"
            ></el-table-column>
            <el-table-column
              label="创建时间"
              align="center"
              prop="createTime"
            ></el-table-column>
            <el-table-column
              label="排序"
              align="center"
              prop="sort"
            ></el-table-column>
            <el-table-column
              v-if="activeName === 'second'"
              label="平台名称"
              align="center"
              prop="appName"
            ></el-table-column>
            <el-table-column
              label="操作人"
              align="center"
              prop="editName"
            ></el-table-column>
            <el-table-column
              label="操作时间"
              align="center"
              prop="updateTime"
            ></el-table-column>
            <el-table-column
              label="操作"
              align="center"
              v-if="activeName === 'second'"
            >
              <template slot-scope="scope">
                <el-button
                  size="small"
                  @click="handleUpdate(scope.$index, scope.row)"
                  type="text"
                  icon="el-icon-edit"
                  >修改</el-button
                >
                <el-button
                  size="small"
                  @click="handleDetails(scope.$index, scope.row)"
                  type="text"
                  >删除</el-button
                >
                <el-button
                  size="small"
                  @click="handleLabelProdut(scope.$index, scope.row)"
                  type="text"
                  >配置商品</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination-container">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes,prev, pager, next,jumper"
            :page-size="listQuery.size"
            :page-sizes="[5, 10, 15]"
            :current-page.sync="listQuery.current"
            :total="total"
          >
          </el-pagination>
        </div>
      </el-tabs>

      <el-dialog
        :title="titleDialog"
        :visible.sync="dialogVisible"
        :before-close="close"
        width="40%"
      >
        <el-form
          :model="appAddForm"
          :rules="rules"
          ref="appAddForm"
          label-width="150px"
        >
          <el-form-item label="行列标题：" prop="labelName">
            <el-input v-model="appAddForm.labelName"></el-input>
          </el-form-item>
          <el-form-item label="排序号:" prop="sort">
            <el-input v-model="appAddForm.sort"></el-input>
          </el-form-item>
          <el-form-item label="平台:" prop="appId">
            <el-select
              clearable
              v-model="appAddForm.appId"
              placeholder="请选择"
            >
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('appAddForm')"
              >提交</el-button
            >
            <el-button
              v-if="!this.appAddForm.id"
              @click="resetForm('appAddForm')"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </el-dialog>
    </basic-container>
  </div>
</template>
<script>
import "@/styles/index.scss";
import {
  operatePage,
  appPage,
  delLabel,
  addLabel,
  updateLabel,
} from "@/api/home/banner";
import { platformAll, getStore } from "@/api/rms/rmsstore";

const defaultAppAddForm = {
  id: null,
  labelName: null,
  sort: null,
  appId: null,
};

export default {
  data() {
    return {
      activeName: "first",
      list: [],
      total: 0,
      listQuery: {
        current: 1,
        size: 10,
        storeId: null,
      },
      appQuery: {
        current: 1,
        size: 10,
        appId: null,
      },
      options: [],
      titleDialog: "添加",
      dialogVisible: false,
      appAddForm: Object.assign({}, defaultAppAddForm),
      rules: {
        sort: [{ required: true, message: "请输入排序号", trigger: "blur" }],
        labelName: [{ required: true, messge: "请输入标题", trigger: "blur" }],
        appId: [{ required: true, messge: "请选择平台", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getOperatePage();
    this.getStoreList();
  },
  methods: {
    getPlatformAll() {
      platformAll().then((res) => {
        this.options = res.data.data;
      });
    },
    getStoreList() {
      getStore().then((res) => {
        this.options = res.data.data;
      });
    },
    handleClickTab(tab, event) {
      this.activeName = tab.name;
      if (this.activeName === "first") {
        this.listQuery.storeId = null;
        this.getStoreList();
        this.getOperatePage();
      } else if (this.activeName === "second") {
        this.appQuery.appId = null;
        this.getAppPage();
        this.getPlatformAll();
      }
    },
    handleSizeChange(val) {
      if (this.activeName === "first") {
        this.listQuery.size = val;
        this.getOperatePage();
      } else if (this.activeName === "second") {
        this.appQuery.size = val;
        this.getAppPage();
      }
    },
    handleCurrentChange(val) {
      if (this.activeName === "first") {
        this.listQuery.current = val;
        this.getOperatePage();
      } else if (this.activeName === "second") {
        this.appQuery.current = val;
        this.getAppPage();
      }
    },
    getOperatePage() {
      operatePage(this.listQuery).then((res) => {
        this.list = res.data.data.records;
        this.total = res.data.data.total;
      });
    },
    getAppPage() {
      appPage(this.appQuery).then((res) => {
        this.list = res.data.data.records;
        this.total = res.data.data.total;
      });
    },
    handleSecrch() {
      if (this.activeName === "first") {
        this.getOperatePage();
      } else if (this.activeName === "second") {
        this.getAppPage();
      }
    },
    handleResetSearch() {
      if (this.activeName === "first") {
        this.listQuery.storeId = null;
        this.getOperatePage();
      } else if (this.activeName === "second") {
        this.appQuery.appId = null;
        this.getAppPage();
      }
    },
    handleAdd() {
      this.dialogVisible = true;
      this.titleDialog = "添加";
    },
    handleUpdate(index, row) {
      this.dialogVisible = true;
      this.titleDialog = "编辑";
      this.appAddForm = Object.assign({}, row);
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.titleDialog === "编辑") {
            updateLabel(this.appAddForm).then((result) => {
              if (result.data.code == 200) {
                this.$message({
                  message: result.data.msg,
                  type: "success",
                  duration: 1000,
                });
                this.getAppPage();
                this.close();
              } else {
                this.$message({
                  message: result.data.msg,
                  type: "warning",
                  duration: 1000,
                });
              }
            });
          } else {
            addLabel(this.appAddForm).then((result) => {
              if (result.data.code == 200) {
                this.$message({
                  message: result.data.msg,
                  type: "success",
                  duration: 1000,
                });
                this.getAppPage();
                this.close();
              } else {
                this.$message({
                  message: result.data.msg,
                  type: "warning",
                  duration: 1000,
                });
              }
            });
          }
        }
      });
    },
    close() {
      this.dialogVisible = false;
      this.appAddForm = Object.assign({}, defaultAppAddForm);
    },
    handleDetails(index, row) {
      let that = this;
      that
        .$confirm("是否确认删除ID为" + row.id, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(function () {
          delLabel(row.id).then((res) => {
            if (res.data.code === 200) {
              that.$message({
                message: res.data.msg,
                type: "success",
                duration: 1000,
              });
              that.getAppPage();
            } else {
              that.$message({
                message: res.data.msg,
                type: "warning",
                duration: 1000,
              });
            }
          });
        });
    },
    handleLabelProdut(index, row) {
      this.$router.push({
        path: "/home/label/add",
        query: {
          labelId: row.id,
          appId: row.appId,
        },
      });
    },
  },
};
</script>